using System.Collections.Generic;
using UnityEngine;

public class Inventory_Player : Inventory_Base
{
    [Header("Player")]
    public int gold = 100;
    public List<Inventory_EquipmentSlot> equipList = new();

    [Header("Quick Item Slot")]
    public Inventory_Item[] quickItems = new Inventory_Item[2];

    public void SetQuickItemInSlot(int slotNumber, Inventory_Item itemToSet)
    {
        quickItems[slotNumber - 1] = itemToSet;
        OnTriggerUpdateUI();
    }
    public void TryUseQuickSlotItem(int slotNumber)
    {
        int slotIndex = slotNumber - 1;
        var itemToUse = quickItems[slotIndex];
        if (itemToUse == null)
            return;

        itemToUse ??= itemList.Find(target => target.itemData.itemType == ItemType.Consumable);

        TryUseConsumable(itemToUse);

        if (FindItem(itemToUse) == null)
            itemToUse = FindInventoryItem(itemToUse, itemList);

        itemToUse ??= itemList.Find(target => target.itemData.itemType == ItemType.Consumable);
        quickItems[slotIndex] = itemToUse;

        OnTriggerUpdateUI();
    }
    //尝试装备物品
    public void TryEquipSlot(Inventory_Item itemToEquip)
    {
        var inventoryItem = FindInventoryItem(itemToEquip,itemList);

        if (inventoryItem == null)
            return;
        //在unity中先定义列表的slotType,否则无法判断种类
        var matchSlots = equipList.FindAll(slot => slot.slotType == inventoryItem.itemData.itemType);

        if (matchSlots.Count == 0)
            return;

        foreach (var slot in matchSlots)
        {
            //如果装备槽没有装备物品
            if (slot.HasPlugSlot() == false)
            {
                EquipItem(inventoryItem, slot);
                return;
            }
        }

        //如果已经装备，点击装备时将当前搜索到的对应装备槽的第一个槽替换成点击的物品
        var equipmentSlot = matchSlots[0];

        UnEquipItem(equipmentSlot.equipedItem, equipmentSlot != null);

        EquipItem(inventoryItem, equipmentSlot);
    }

    //装备物品
    private void EquipItem(Inventory_Item item, Inventory_EquipmentSlot slot)
    {
        float saveHealthPercent = player.playerHealth.GetCurrentHealthPercent();

        item.itemEffect?.Subscribe(player);

        slot.equipedItem = item;
        slot.equipedItem.AddModifier(player.playerStats);

        player.playerHealth.SetCurrentHealthPercent(saveHealthPercent);

        RemoveOneItemInOneSlot(item);
    }

    //取消装备物品
    public void UnEquipItem(Inventory_Item itemToUnEquip, bool isNeedRepalcingEquipSlot = false)
    {
        //如果库存已经满了，并且不需要取代已经装备了的装备
        //解释：如果isNeedRepalcingEquipSlot==true, 则你需要取代装备槽的物品，
        //      此时isNeedRepalcingEquipSlot == false的结果为false，判断不执行
        //      并不需要判断库存是否已经满了，因为取代卡槽会进行装备互换，此时并不需要额外的卡槽开销
        if (CanAddItem(itemToUnEquip) == false && isNeedRepalcingEquipSlot == false)
            return;

        float saveHealthPercent = player.playerHealth.GetCurrentHealthPercent();

        Inventory_EquipmentSlot slotItem = equipList.Find(match => match.equipedItem == itemToUnEquip);

        if (slotItem == null)
            return;

        slotItem.equipedItem.itemEffect?.UnSubscribe();

        slotItem.equipedItem.RemoveModifier(player.playerStats);
        slotItem.equipedItem = null;
        AddItem(itemToUnEquip);

        player.playerHealth.SetCurrentHealthPercent(saveHealthPercent);
    }

    public override void LoadData(GameData gameData)
    {
        gold = gameData.gold;

        foreach (var item in gameData.inventory)
        {
            string saveId = item.Key;
            int saveStackSize = item.Value;

            ItemDataSO itemData = itemDataBase.GetItemData(saveId);
            if (itemData == null)
                continue;

            for (int i = 0; i < saveStackSize; i++)
            {
                Inventory_Item itemToLoad = new(itemData);
                AddItem(itemToLoad);
            }
        }
        
        for (int i = 0; i < gameData.equipment.Count; i++)
        {
            if (gameData.equipment[i] != "null")
            {
                ItemDataSO itemData = itemDataBase.GetItemData(gameData.equipment[i]);
                Inventory_EquipmentSlot equipSlot = new(itemData);
                equipList[i] = equipSlot;
                equipList[i].equipedItem.itemEffect?.Subscribe(player);
                equipList[i].equipedItem.AddModifier(player.playerStats);
            }
        }
        
    }
    public override void SaveData(ref GameData gameData)
    {
        gameData.gold = gold;

        gameData.inventory.Clear();
        foreach (var item in itemList)
        {
            if (item == null || item.itemData == null)
                continue;

            string itemId = item.itemData.saveId;
            if (gameData.inventory.ContainsKey(itemId) == false)
                gameData.inventory[itemId] = 0;
            gameData.inventory[itemId] += item.stackSize;
        }

        //
        gameData.equipment.Clear();
        foreach (var equip in equipList)
        {
            if (equip == null || equip.equipedItem == null || equip.equipedItem.itemData == null)
                gameData.equipment.Add("null");
            else
                gameData.equipment.Add(equip.equipedItem.itemData.saveId);
        }
    }
}
